package com.xjw.dao.util;

import java.util.ArrayList;
import java.util.List;

/**      
 * 项目名称：spring-project  
 * 实现功能：  
 * 类名称：PageList   
 * 版权所有：
 * 创建人：徐纪伟 
 * E-mail: 289045706@qq.com
 * 创建时间：2017年1月12日 下午3:56:30   	
 * 修改人：   
 * 修改时间：   
 * 修改备注：   
 * @version    
 */
public class PageList<T> extends ArrayList<T> {

    /** serialVersionUID */
    private static final long serialVersionUID = 2929544895733609661L;

    /** 当前页码  */
    private int               page;
    /** 上一页码   */
    private int               prev;
    /** 下一页码   */
    private int               next;
    /** 查询结果总条数  */
    private long              amount;
    /** 分页大小  */
    private int               pageSize;
    /** 总页数  */
    private int               totalPage;
    /** 滑动显示页码列表  */
    private List<Integer>     slidePages;

    /**
     * 构造分页结果集
     * @param list     数据列表
     * @param page  页码
     * @param amount   总数量 
     * @param pageSize 分页大小
     */
    public PageList(int page, List<T> list, long amount, int pageSize) {

        super(list);
        this.page = page;
        this.amount = amount;
        this.pageSize = pageSize;
        this.totalPage = (int) Math.ceil((double) amount / pageSize);
        
        if (this.hasNext()) {
            this.next = this.page + 1;
        }
        if (this.hasPrev()) {
            this.prev = this.page - 1;
        }

        slidePages = new ArrayList<Integer>();
        int slideStart = 1;
        if (this.page >5) {
            slideStart = this.page - (this.page % 5);
        }
        int slideEnd = slideStart + 10;
        if (slideEnd > this.totalPage) {
            slideEnd = this.totalPage;
        }
        for (int i = slideStart; i <= slideEnd; i++) {
            slidePages.add(i);
        }
    }

    /**
     * 返回分页结果集：保持分页相关属性不变，替换对象列表
     * @param list    列表
     * @return PageList<T>
     */
    public <I> PageList<I> replaceList(List<I> list) {
        
        return new PageList<I>(this.page, list, this.amount, this.pageSize);
    }

    /**
     * 是否有下一页
     * @return
     */
    public boolean hasNext() {

        return this.page < this.totalPage;
    }

    /**
     * 是否有上一页
     * @return
     */
    public boolean hasPrev() {

        return this.page > 1;
    }

    //------------------ getters and setters ----------------

    /**
     * Getter method for property <tt>page</tt>.
     * 
     * @return property value of page
     */
    public int getPage() {
        return page;
    }

    /**
     * Setter method for property <tt>page</tt>.
     * 
     * @param page value to be assigned to property page
     */
    public void setPage(int page) {
        this.page = page;
    }

    /**
     * Getter method for property <tt>prev</tt>.
     * 
     * @return property value of prev
     */
    public int getPrev() {
        return prev;
    }

    /**
     * Setter method for property <tt>prev</tt>.
     * 
     * @param prev value to be assigned to property prev
     */
    public void setPrev(int prev) {
        this.prev = prev;
    }

    /**
     * Getter method for property <tt>next</tt>.
     * 
     * @return property value of next
     */
    public int getNext() {
        return next;
    }

    /**
     * Setter method for property <tt>next</tt>.
     * 
     * @param next value to be assigned to property next
     */
    public void setNext(int next) {
        this.next = next;
    }

    /**
     * Getter method for property <tt>amount</tt>.
     * 
     * @return property value of amount
     */
    public long getAmount() {
        return amount;
    }

    /**
     * Setter method for property <tt>amount</tt>.
     * 
     * @param amount value to be assigned to property amount
     */
    public void setAmount(long amount) {
        this.amount = amount;
    }

    /**
     * Getter method for property <tt>pageSize</tt>.
     * 
     * @return property value of pageSize
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * Setter method for property <tt>pageSize</tt>.
     * 
     * @param pageSize value to be assigned to property pageSize
     */
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * Getter method for property <tt>totalPage</tt>.
     * 
     * @return property value of totalPage
     */
    public int getTotalPage() {
        return totalPage;
    }

    /**
     * Setter method for property <tt>totalPage</tt>.
     * 
     * @param totalPage value to be assigned to property totalPage
     */
    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    /**
     * Getter method for property <tt>slidePages</tt>.
     * 
     * @return property value of slidePages
     */
    public List<Integer> getSlidePages() {
        return slidePages;
    }

    /**
     * Setter method for property <tt>slidePages</tt>.
     * 
     * @param slidePages value to be assigned to property slidePages
     */
    public void setSlidePages(List<Integer> slidePages) {
        this.slidePages = slidePages;
    }

}